package wh.路径总和;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2022/4/21 20:18
 * @description:判断该树中是否存在 根节点到叶子节点 的路径，这条路径上所有节点值相加等于目标和
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1,new TreeNode(2,new TreeNode(4),new TreeNode(5)),new TreeNode(3,new TreeNode(6),new TreeNode(7)));
        boolean b = hasPathSum(root, 10);
        System.out.println(b);
    }

    //递归解法 妙不可言
    public static boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        if (root.left == null && root.right == null) {
            return targetSum == root.val;
        }
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }
}
